#include "PyProUIDialog.h"
#include <ProUIDialog.h>
#include "PyProError.h"
#include "BindFunction.h"


using namespace pyBind;


static PyObject* PyProUIDialogCreate( PyObject *self, PyObject *args )
{
	char* dialog_name;	
	char* resource = NULL;

	if(!PyArg_ParseTuple(args,"s|s:ProUIDialogCreate", 
		&dialog_name,
		&resource
		))
		return NULL;

	if(strlen(dialog_name) == 0)
	{
		PyErr_SetString((PyObject *)&PyExc_NameError,"name is empty.");
		return NULL;
	}

	ProErr err = ProUIDialogCreate(dialog_name,resource);
	if(PRO_TK_NO_ERROR == err)
	{
		int l = strlen(dialog_name) + 1;
		
		PyProUIDialog * newDlg = PyObject_New(PyProUIDialog,&PyProUIDialogType);
		newDlg->dialog = new char[l];
		strcpy_s(newDlg->dialog,l,dialog_name);
		return newDlg; 
	}
	else
	{
		PyErr_SetObject((PyObject *)&pyProErrorType,PyLong_FromLong(err));
		return NULL;
	}
}

static PyObject* PyProUIDialogActivate( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_ret|_adr>  >(ProUIDialogActivate,self,args);
}

static PyObject* PyProUIDialogExit( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_in|_val>  >(ProUIDialogExit,self,args);
}

static PyObject* PyProUIDialogDestroy( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag>   >(ProUIDialogDestroy,self,args);
}

static PyObject* PyProUIDialogDefaultbuttonSet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_in|_val,StringTag>  >(ProUIDialogDefaultbuttonSet,self,args);
}
static PyObject* PyProUIDialogTitleSet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_in|_val,WStringTag>  >(ProUIDialogTitleSet,self,args);
}

static PyObject* PyProUIDialogCloseActionSet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_in|_val>,Ag<_in|_val> >(ProUIDialogCloseActionSet,self,args);
}

static PyObject* PyProUIDialogActivateActionSet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_in|_val>,Ag<_in|_val> >(ProUIDialogActivateActionSet,self,args);
}

static PyObject* PyProUIDialogPopupmenuSet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_in|_val,StringTag> >(ProUIDialogPopupmenuSet,self,args);
}

static PyObject* PyProUIDialogPopupmenuGet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_ret|_adr,ProStringTag> >(ProUIDialogPopupmenuGet,self,args);
}

static PyObject* PyProUIDialogHorzpointSet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_in|_val> >(ProUIDialogHorzpointSet,self,args);
}

static PyObject* PyProUIDialogHorzpointGet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_ret|_adr> >(ProUIDialogHorzpointGet,self,args);
}

static PyObject* PyProUIDialogHorzmethodSet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_in|_val> >(ProUIDialogHorzmethodSet,self,args);
}

static PyObject* PyProUIDialogHorzmethodGet( PyObject *self, PyObject *args )
{
	return PyFun< Ag<_this,UIDialogTag> ,Ag<_ret|_adr> >(ProUIDialogHorzmethodGet,self,args);
}


static PyMethodDef ProUIDialog_methods[] = {
	{"Create", PyProUIDialogCreate, METH_VARARGS|METH_STATIC,"Create UIDialog."},
	{"Activate", PyProUIDialogActivate, METH_VARARGS,"Activate UIDialog."},
	{"Exit", PyProUIDialogExit, METH_VARARGS,"Exit UIDialog."},
	{"Destroy", PyProUIDialogDestroy, METH_VARARGS,"Destroy UIDialog."},
	{"DefaultbuttonSet", PyProUIDialogDefaultbuttonSet, METH_VARARGS,"UIDialog DefaultbuttonSet."},
	{"TitleSet", PyProUIDialogTitleSet, METH_VARARGS,"UIDialog TitleSet."},
	{"CloseActionSet", PyProUIDialogCloseActionSet, METH_VARARGS,"UIDialog CloseActionSet."},
	{"ActivateActionSet", PyProUIDialogActivateActionSet, METH_VARARGS,"UIDialog ActivateActionSet."},
	{"PopupmenuSet", PyProUIDialogPopupmenuSet, METH_VARARGS,"UIDialog PopupmenuSet."},
	{"PopupmenuGet", PyProUIDialogPopupmenuGet, METH_VARARGS,"UIDialog PopupmenuGet."},
	{"HorzpointSet", PyProUIDialogHorzpointSet, METH_VARARGS,"UIDialog HorzpointSet."},
	{"HorzpointGet", PyProUIDialogHorzpointGet, METH_VARARGS,"UIDialog HorzpointGet."},
	{"HorzmethodSet", PyProUIDialogHorzmethodSet, METH_VARARGS,"UIDialog HorzmethodSet."},
	{"HorzmethodGet", PyProUIDialogHorzmethodGet, METH_VARARGS,"UIDialog HorzmethodGet."},

	{NULL}  /* Sentinel */
};


static PyObject *
PyProUIDialog_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
	PyProUIDialog *self;

	self = (PyProUIDialog *)type->tp_alloc(type, 0);
	if (!self)
		return NULL;
	
	self->dialog = 0;

	return (PyObject *)self;
}

static void
PyProUIDialog_dealloc(PyProUIDialog *self)
{
	ProUIDialogDestroy(self->dialog);
	delete[] self->dialog;
}

PyTypeObject PyProUIDialogType = {
	PyVarObject_HEAD_INIT(NULL, 0)
	"Protk.UIDialog",             /* tp_name */
	sizeof(PyProUIDialog),			/* tp_basicsize */
	0,                         /* tp_itemsize */
	(destructor)PyProUIDialog_dealloc,               /* tp_dealloc */
	0,                         /* tp_print */
	0,                         /* tp_getattr */
	0,                         /* tp_setattr */
	0,                         /* tp_reserved */
	0,                         /* tp_repr */
	0,                         /* tp_as_number */
	0,                         /* tp_as_sequence */
	0,                         /* tp_as_mapping */
	0,                         /* tp_hash  */
	0,                         /* tp_call */
	0,                         /* tp_str */
	0,                         /* tp_getattro */
	0,                         /* tp_setattro */
	0,                         /* tp_as_buffer */
	Py_TPFLAGS_DEFAULT,        /* tp_flags */
	"ProUIDialog objects",           /* tp_doc */
	0,		               /* tp_traverse */
	0,		               /* tp_clear */
	0,		               /* tp_richcompare */
	0,		               /* tp_weaklistoffset */
	0,		               /* tp_iter */
	0,		               /* tp_iternext */
	ProUIDialog_methods,             /* tp_methods */
	0,             /* tp_members */
	0,                         /* tp_getset */
	0,                         /* tp_base */
	0,                         /* tp_dict */
	0,                         /* tp_descr_get */
	0,                         /* tp_descr_set */
	0,                         /* tp_dictoffset */
	0,      /* tp_init */
	0,                         /* tp_alloc */
	PyProUIDialog_new,                 /* tp_new */

};



int PyInit_ProUIDialog( PyObject* m )
{
	PyType_Ready(&PyProUIDialogType);
	Py_INCREF(&PyProUIDialogType);
	return PyModule_AddObject(m, "UIDialog", (PyObject *)&PyProUIDialogType);
}

int UIDialogTag::PyValueToTkValue()
{
	PyProUIDialog * dlg = (PyProUIDialog *)pyval;
	int l = strlen(dlg->dialog) + 1;

	if(!m_protkval)
	{
		m_protkval = new char[l];
		strcpy_s(m_protkval,l,dlg->dialog);
	}

	return 1;
}

UIDialogTag::~UIDialogTag()
{
	delete[] m_protkval;
}

UIDialogTag::UIDialogTag() :m_protkval(0)
{

}
